package com.aidex.system.mapper;

import com.aidex.common.core.domain.entity.SysDept;
import com.aidex.common.core.domain.entity.SysMenu;
import com.aidex.common.core.mapper.BasicTreeMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface SysDeptMapper extends BasicTreeMapper<SysDept> {

    @Override
    @Select("select * from sys_dept t ${ew.customSqlSegment}")
    @Result(column = "id", property = "children", one = @One(select = "com.aidex.system.mapper.SysDeptMapper.selectListByParentId"))
    @Result(column = "id", property = "id")
    List<SysDept> selectList(@Param("ew") Wrapper<SysDept> queryWrapper);

    @Select("select * from sys_dept t where t.parent_id = #{parentId} and t.del_flag = '0'")
    @Result(column = "id", property = "children", one = @One(select = "com.aidex.system.mapper.SysDeptMapper.selectListByParentId"))
    @Result(column = "id", property = "id")
    List<SysDept> selectListByParentId(@Param("parentId") String parentId);

    @Select("select count(id) from sys_dept t where t.status = 0 and t.del_flag = '0' and t.parent_ids like concat(#{parentIds},'%') and t.parent_ids &lt;&gt; #{parentIds}")
    Integer selectNormalChildByParentIds(@Param("parentIds") String parentIds);

    @Select("select count(1) from sys_user where dept_id = #{deptId} and del_flag = '0'")
    Integer checkDeptExistUser(@Param("deptId") String deptId);

    @Select("select max(t.tree_sort) + 120 from sys_dept t where t.del_flag = 0 and t.parent_id = #{parentId}")
    Integer findMaxSort(@Param("parentId") String parentId);

    @Select("select t.id, t.parent_id as 'parentId', t.parent_ids as 'parentIds', t.dept_name as 'deptName', t.dept_name as 'subtitle', t.dept_type as 'deptType', 'n' AS treeLeaf, 0 AS groupSort, t.tree_sorts  as 'treeSorts', t.dept_pinyin as 'deptPinyin' from sys_dept t where t.del_flag = 0 and t.parent_id = #{parentId}")
    List<SysDept> findDeptChildListByParentId(@Param("parentId") String parentId);

    @Select("select d.id, t.dept_id as 'parentId', concat(d.parent_ids,'/',t.id) as 'parentIds', t.NAME as 'deptName', d.dept_name as 'subtitle', 'user' as 'deptType', 'y' AS treeLeaf, 1 AS groupSort, t.sort  as 'treeSorts', t.user_pinyin as 'deptPinyin' from sys_user t left join sys_dept d on t.dept_id = d.id where t.del_flag = 0 and t.dept_id = #{parentId}")
    List<SysDept> findUserChildListByParentId(@Param("parentId") String parentId);

    @Select("select count(1) from sys_dept where parent_ids  like concat(concat('%',#{id}),'/%') AND del_flag = 0 AND dept_type = #{deptType}")
    int searchChildrenTypeByParentId(SysDept sysDept);
}
